﻿using System;
using System.Data;
using System.Configuration;
using System.Windows.Forms;

using System.Data.OleDb;

/// <summary>
/// AccessHelper 的摘要说明
/// </summary>
namespace SmallERP.DAL
{
    public class AccessHelper
    {
        public  static OleDbConnection conn = new OleDbConnection();
        public static OleDbCommand commd = new OleDbCommand();
        public AccessHelper()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
           
        }
        public static string pathdb = Application.StartupPath + @"\MDB\smallerp.mdb";
        static string connStr = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + pathdb + ";Jet OLEDB:Database Password=123";
        static string MyConnstr = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + pathdb + ";Jet OLEDB:Database Password=123";
        /// <summary>
        /// 打开数据库
        /// </summary>
        public  static void openConnection()
        {
            if (conn.State == ConnectionState.Closed)
            {
                conn.ConnectionString = connStr;
                commd.Connection = conn;
                try
                {
                    conn.Open();
                }
                catch (Exception e)
                { throw new Exception(e.Message); }
            }
        }
        public static OleDbConnection OpenMyConnection()
        {
            OleDbConnection myconn = new OleDbConnection(connStr);
            //OleDbConnection.ConnectionString = MyConnstr;
               
                try
                {
                    myconn.Open();
                }
                catch (Exception e)
                { throw new Exception(e.Message); }
                return myconn;
            
        }
        public static void CloseMyConnection(OleDbConnection conn)
        {
            //OleDbConnection myconn = new OleDbConnection(connStr);
            //OleDbConnection.ConnectionString = MyConnstr;

            try
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
            catch (Exception e)
            { throw new Exception(e.Message); }
            

        }
        /// <summary>
        /// 关闭数据库
        /// </summary>
        public  static void closeConnection()
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
                conn.Dispose();
                commd.Dispose();
            }
        }
        /// <summary>
        /// 执行sql语句
        /// </summary>
        /// <param name="sqlstr"></param>
        public static void excuteSql(string sqlstr)
        {
            try
            {
                openConnection();
                commd.CommandType = CommandType.Text;
                commd.CommandText = sqlstr;
                commd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            { closeConnection(); }
        }
        /// <summary>
        /// 返回指定sql语句的OleDbDataReader对象，使用时请注意关闭这个对象。
        /// </summary>
        /// <param name="sqlstr"></param>
        /// <returns></returns>
        public static OleDbDataReader dataReader(string sqlstr)
        {
            OleDbDataReader dr = null;
            try
            {
                openConnection();
                commd.CommandText = sqlstr;
                commd.CommandType = CommandType.Text;
                dr = commd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch
            {
                try
                {
                    dr.Close();
                    closeConnection();
                }
                catch { }
            }
            return dr;
        }
        /// <summary>
        /// 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭
        /// </summary>
        /// <param name="sqlstr"></param>
        /// <param name="dr"></param>
        public static void dataReader(string sqlstr, ref OleDbDataReader dr)
        {
            try
            {
                openConnection();
                commd.CommandText = sqlstr;
                commd.CommandType = CommandType.Text;
                dr = commd.ExecuteReader(CommandBehavior.CloseConnection);
                
            }
            catch
            {
                try
                {
                    if (dr != null && !dr.IsClosed)
                        dr.Close();
                }
                catch
                {
                }
                finally
                {
                    closeConnection();
                }
            }
        }

        /// <summary>
        /// 返回指定受影响的行数
        /// </summary>
        /// <param name="sqlstr"></param>
        /// <param name="dr"></param>
        public static int ExecuteNonQuery(string sqlstr)
        {
            int rtnInt = 0;
            try
            {
                openConnection();
                commd.CommandText = sqlstr;
                commd.CommandType = CommandType.Text;
                rtnInt = commd.ExecuteNonQuery();

            }
            catch
            {

            }
            finally
            {
                closeConnection();
            }
            return rtnInt;
        }

        public static int ExecuteNonQuery(string sqlStr, params OleDbParameter[] cmdParms)
        {
            int rtnInt = 0;
            openConnection();
            commd.CommandText = sqlStr;
            commd.CommandType = CommandType.Text;
          
         
            if (cmdParms != null)
            {
                foreach (OleDbParameter parm in cmdParms)
                    commd.Parameters.Add(parm);
            }
            rtnInt = commd.ExecuteNonQuery();
            return rtnInt;
        }
        public static int ExecuteNonQuery(string sqlStr,OleDbConnection conn, OleDbTransaction tran,params OleDbParameter[] cmdParms)
        {
            int rtnInt = 0;
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            OleDbCommand olecommdCommand = conn.CreateCommand();
            olecommdCommand.CommandText = sqlStr;
            olecommdCommand.CommandType = CommandType.Text;
            olecommdCommand.Transaction = tran;


            if (cmdParms != null)
            {
                foreach (OleDbParameter parm in cmdParms)
                    olecommdCommand.Parameters.Add(parm);
            }
            rtnInt = olecommdCommand.ExecuteNonQuery();
            return rtnInt;
        }
        /// <summary>
        /// 执行查询，并返回查询所返回的结果集中第一行的第一列。忽略其他列或行。
        /// </summary>
        /// <param name="sqlstr"></param>
        /// <param name="dr"></param>
        public static object ExecuteScalar(string sqlstr)
        {
            object obj = 0;
            try
            {
                openConnection();
                commd.CommandText = sqlstr;
                commd.CommandType = CommandType.Text;
                obj = commd.ExecuteScalar();

            }
            catch
            {
                //return -1;
            }
            finally
            {
                closeConnection();
            }
            return obj;
        }
        /// <summary>
        /// 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭
        /// </summary>
        /// <param name="sqlstr"></param>
        /// <param name="dr"></param>
        public static object ExecuteScalar(string sqlstr,OleDbConnection olecnn, OleDbTransaction tran)
        {
            object rtnInt = 0;
            try
            {
                //openConnection();
                if (olecnn.State == ConnectionState.Closed)
                {
                    olecnn.Open();
                }
                OleDbCommand olecomm = olecnn.CreateCommand();
                olecomm.CommandText = sqlstr;
                olecomm.CommandType = CommandType.Text;
                olecomm.Transaction = tran;
                rtnInt = (object)olecomm.ExecuteScalar();

            }
            catch
            {

            }
            finally
            {
                ///closeConnection();
            }
            return rtnInt;
        }
        /// <summary>
        /// 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭
        /// </summary>
        /// <param name="sqlstr"></param>
        /// <param name="dr"></param>
        public static object ExecuteScalar(string sqlstr, OleDbConnection olecnn, OleDbTransaction tran, params  OleDbParameter[] cmdParms)
        {
            object rtnInt = 0;
            try
            {
                //openConnection();
                if (olecnn.State == ConnectionState.Closed)
                {
                    olecnn.Open();
                }
                OleDbCommand olecomm = olecnn.CreateCommand();
                olecomm.CommandText = sqlstr;
                olecomm.CommandType = CommandType.Text;
                olecomm.Transaction = tran;
                if (cmdParms != null)
                {
                    foreach (OleDbParameter parm in cmdParms)
                        commd.Parameters.Add(parm);
                }
                rtnInt = (object)olecomm.ExecuteScalar();

            }
            catch
            {

            }
            finally
            {
                ///closeConnection();
            }
            return rtnInt;
        }
        /// <summary>
        /// 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭
        /// </summary>
        /// <param name="sqlstr"></param>
        /// <param name="dr"></param>
        public static int ExecuteNonQuery(string sqlstr,OleDbConnection olecnn,OleDbTransaction tran)
        {
            int rtnInt = 0;
            try
            {
                //openConnection();
                //comm.CommandText = sqlstr;
                //comm.CommandType = CommandType.Text;
                //comm.Transaction = tran;
                if (olecnn.State == ConnectionState.Closed)
                {
                    olecnn.Open();
                }
                OleDbCommand olecomm = olecnn.CreateCommand();
                olecomm.CommandText = sqlstr;
                olecomm.CommandType = CommandType.Text;
                olecomm.Transaction = tran;
                rtnInt = olecomm.ExecuteNonQuery();
                //olecomm.UpdatedRowSource;
            }
            catch
            {

            }
            finally
            {
                //closeConnection();
            }
            return rtnInt;
        }
        /// <summary>
        /// 返回指定sql语句的dataset
        /// </summary>
        /// <param name="sqlstr"></param>
        /// <returns></returns>
        public static DataSet dataSet(string sqlstr)
        {
            DataSet ds = new DataSet();
            OleDbDataAdapter da = new OleDbDataAdapter();
            try
            {
                openConnection();
                commd.CommandType = CommandType.Text;
                commd.CommandText = sqlstr;
                da.SelectCommand = commd;
                da.Fill(ds);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                closeConnection();
            }
            return ds;
        }
        /// <summary>
        /// 返回指定sql语句的dataset
        /// </summary>
        /// <param name="sqlstr"></param>
        /// <param name="ds"></param>
        public static void dataSet(string sqlstr, ref DataSet ds)
        {
            OleDbDataAdapter da = new OleDbDataAdapter();
            try
            {
                openConnection();
                commd.CommandType = CommandType.Text;
                commd.CommandText = sqlstr;
                da.SelectCommand = commd;
                da.Fill(ds);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                closeConnection();
            }
        }
        /// <summary>
        /// 返回指定sql语句的datatable
        /// </summary>
        /// <param name="sqlstr"></param>
        /// <returns></returns>
        public static DataTable dataTable(string sqlstr)
        {
            DataTable dt = new DataTable();
            OleDbDataAdapter da = new OleDbDataAdapter();
            try
            {
                openConnection();
                commd.CommandType = CommandType.Text;
                commd.CommandText = sqlstr;
                da.SelectCommand = commd;
                da.Fill(dt);
                dt.TableName = "table";
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                closeConnection();
            }
            return dt;
        }
        /// <summary>
        /// 返回指定sql语句的datatable
        /// </summary>
        /// <param name="sqlstr"></param>
        /// <returns></returns>
        public static DataTable dataTable(string sqlstr,OleDbConnection olecnn,OleDbTransaction tran)
        {
            DataTable dt = new DataTable();
            OleDbDataAdapter da = new OleDbDataAdapter();
            try
            {
                //openConnection();
                //comm.CommandType = CommandType.Text;
                //comm.CommandText = sqlstr;
                //comm.Transaction = tran;
                //da.SelectCommand = comm;
                if (olecnn.State == ConnectionState.Closed)
                {
                    olecnn.Open();
                }
                OleDbCommand olecomm = olecnn.CreateCommand();
                olecomm.CommandText = sqlstr;
                olecomm.CommandType = CommandType.Text;
                olecomm.Transaction = tran;
                da.SelectCommand = olecomm;
                da.Fill(dt);
                dt.TableName = "table";
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                //closeConnection();
            }
            return dt;
        }
        /// <summary>
        /// 返回指定sql语句的datatable
        /// </summary>
        /// <param name="sqlstr"></param>
        /// <param name="dt"></param>
        public static void dataTable(string sqlstr, ref DataTable dt)
        {
            OleDbDataAdapter da = new OleDbDataAdapter();
            try
            {
                openConnection();
                commd.CommandType = CommandType.Text;
                commd.CommandText = sqlstr;
                da.SelectCommand = commd;
                da.Fill(dt);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                closeConnection();
            }
        }
        /// <summary>
        /// 返回指定sql语句的dataview
        /// </summary>
        /// <param name="sqlstr"></param>
        /// <returns></returns>
        public static DataView dataView(string sqlstr)
        {
            OleDbDataAdapter da = new OleDbDataAdapter();
            DataView dv = new DataView();
            DataSet ds = new DataSet();
            try
            {
                openConnection();
                commd.CommandType = CommandType.Text;
                commd.CommandText = sqlstr;
                da.SelectCommand = commd;
                da.Fill(ds);
                dv = ds.Tables[0].DefaultView;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                closeConnection();
            }
            return dv;
        }
    }
}